package org.functional.demo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Zhang Yu
 * @description: TODO
 * @date 2024/5/14 14:55
 */
public class StreamDemo1 {

    public static void main(String[] args) {
        List<String> names = Arrays.asList("Bob", "Alice", "Tim");
//        List<Person> persons = parse1(names);
        List<Person> persons = parse2(names);
        System.out.println("persons = " + persons);
    }

    static List<Person> parse1(List<String> names) {
        List<Person> persons = new ArrayList<>();
        for (String name : names) {
            persons.add(new Person(name));
        }
        return persons;
    }

    static List<Person> parse2(List<String> names) {
        List<Person> persons = names.stream().map(Person::new).collect(Collectors.toList());
        return persons;
    }


}

class Person {
    String name;
    public Person(String name) {
        this.name = name;
    }
    public String toString() {
        return "Person:" + this.name;
    }
}
